/*     */ package com.zimbra.cs.index.analysis;
/*     */ 
/*     */ import com.google.common.base.Strings;
/*     */ import com.google.common.collect.ImmutableList;
/*     */ import com.google.common.net.InternetDomainName;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.mime.InternetAddress;
/*     */ import com.zimbra.common.mime.InternetAddress.Group;
/*     */ import java.io.IOException;
/*     */ import java.io.StringReader;
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import java.util.Collections;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.LinkedList;
/*     */ import java.util.List;
/*     */ import java.util.Set;
/*     */ import javax.mail.internet.MimeUtility;
/*     */ import org.apache.lucene.analysis.TokenStream;
/*     */ import org.apache.lucene.analysis.Tokenizer;
/*     */ import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class RFC822AddressTokenStream
/*     */   extends TokenStream
/*     */ {
/*  64 */   private final List<String> tokens = new LinkedList();
/*     */   private Iterator<String> itr;
/*  66 */   private final CharTermAttribute termAttr = (CharTermAttribute)addAttribute(CharTermAttribute.class);
/*     */   
/*     */   public RFC822AddressTokenStream(String raw) {
/*  69 */     if (Strings.isNullOrEmpty(raw)) {
/*     */       return;
/*     */     }
/*     */     String decoded;
/*     */     try
/*     */     {
/*  75 */       decoded = MimeUtility.decodeText(raw);
/*     */     } catch (UnsupportedEncodingException e) {
/*  77 */       decoded = raw;
/*     */     }
/*     */     
/*     */ 
/*  81 */     Set<String> emails = new HashSet();
/*  82 */     Tokenizer tokenizer = new AddrCharTokenizer(new StringReader(decoded));
/*  83 */     CharTermAttribute term = (CharTermAttribute)tokenizer.addAttribute(CharTermAttribute.class);
/*     */     try {
/*  85 */       while (tokenizer.incrementToken()) {
/*  86 */         if ((term.length() != 1) || (Character.isLetter(term.charAt(0))))
/*     */         {
/*     */ 
/*  89 */           tokenize(term.toString(), emails); }
/*     */       }
/*  91 */       tokenizer.close();
/*     */     }
/*     */     catch (IOException ignore) {}
/*     */     
/*     */ 
/*     */ 
/*  97 */     for (InternetAddress iaddr : InternetAddress.parseHeader(raw)) {
/*  98 */       tokenize(iaddr, emails);
/*     */     }
/*     */     
/* 101 */     this.itr = this.tokens.iterator();
/*     */   }
/*     */   
/*     */   public RFC822AddressTokenStream(RFC822AddressTokenStream stream) {
/* 105 */     this.tokens.addAll(stream.tokens);
/* 106 */     this.itr = this.tokens.iterator();
/*     */   }
/*     */   
/*     */   private void tokenize(String src, Set<String> emails) {
/* 110 */     add(src);
/* 111 */     int at = src.lastIndexOf('@');
/* 112 */     if (at <= 0) {
/* 113 */       return;
/*     */     }
/* 115 */     emails.add(src);
/*     */     
/*     */ 
/* 118 */     String localpart = src.substring(0, at);
/* 119 */     add(localpart);
/*     */     
/*     */ 
/* 122 */     if (localpart.indexOf('.') > 0) {
/* 123 */       for (String part : localpart.split("\\.")) {
/* 124 */         add(part);
/*     */       }
/*     */     }
/*     */     
/* 128 */     if (src.endsWith("@")) {
/* 129 */       return;
/*     */     }
/* 131 */     String domain = src.substring(at + 1);
/* 132 */     add("@" + domain);
/* 133 */     add(domain);
/*     */     try
/*     */     {
/* 136 */       String top = (String)InternetDomainName.fromLenient(domain).topPrivateDomain().parts().get(0);
/* 137 */       add(top);
/* 138 */       add("@" + top);
/*     */     }
/*     */     catch (IllegalArgumentException ignore) {}catch (IllegalStateException ignore) {}
/*     */   }
/*     */   
/*     */ 
/*     */   private void tokenize(InternetAddress iaddr, Set<String> emails)
/*     */   {
/* 146 */     if ((iaddr instanceof InternetAddress.Group)) {
/* 147 */       InternetAddress.Group group = (InternetAddress.Group)iaddr;
/* 148 */       for (InternetAddress member : group.getMembers()) {
/* 149 */         tokenize(member, emails);
/*     */       }
/*     */     } else {
/* 152 */       String email = iaddr.getAddress();
/* 153 */       if (!Strings.isNullOrEmpty(email)) {
/* 154 */         email = email.toLowerCase();
/* 155 */         if (!emails.contains(email)) {
/* 156 */           tokenize(email, emails);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private void add(String token) {
/* 163 */     if ((token.length() <= LC.zimbra_index_rfc822address_max_token_length.intValue()) && (this.tokens.size() < LC.zimbra_index_rfc822address_max_token_count.intValue()))
/*     */     {
/* 165 */       this.tokens.add(token);
/*     */     }
/*     */   }
/*     */   
/*     */   public boolean incrementToken() throws IOException
/*     */   {
/* 171 */     if (this.itr.hasNext()) {
/* 172 */       this.termAttr.setEmpty().append((String)this.itr.next());
/* 173 */       return true;
/*     */     }
/* 175 */     return false;
/*     */   }
/*     */   
/*     */ 
/*     */   public void reset()
/*     */   {
/* 181 */     this.itr = this.tokens.iterator();
/*     */   }
/*     */   
/*     */   public void close()
/*     */   {
/* 186 */     this.tokens.clear();
/*     */   }
/*     */   
/*     */   public List<String> getAllTokens() {
/* 190 */     return Collections.unmodifiableList(this.tokens);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/index/analysis/RFC822AddressTokenStream.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */